
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;

import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;


public class membershipFrame extends JFrame{
	
	// GUI
	private backgroundPanel panel;
	// table
	private JScrollPane scrollPane;
	private String[] columns = {"Name", "MemberID", "Current State", "Start Time"};;
	private JTable memberTable;
	private Vector<member> members;
	// popup menu
	private JPopupMenu popupMenu;
	private JMenuItem addMemberMenuItem;
	private JMenuItem deleteMemberMenuItem;
	private JMenuItem searchMemberMenuItem;
	private JMenuItem editMemberMenuItem;
	
	// handlers
	private AddHandler addHandler;
	private EditHandler editHandler;
	private SearchHandler searchHandler;
	private DeleteHandler deleteHandler;
	
	// data control
	private memberManager mm;
	private MyTableModel tableModel;
	
	public membershipFrame(memberManager initMm){
		mm = initMm;
		members = mm.getMembers();
		System.out.println(members.size());
		layoutGUI();
	}
	
	public void layoutGUI(){
		this.setTitle("Membership Management");
		this.setResizable(false);
		this.setSize(560, 480);
		panel = new backgroundPanel("background6.jpg",0,0,560,480);

		Vector<String[]> data = new Vector<String[]>();
		for (int i=0; i<members.size(); i++){
			String[] line = new String[4];
			line[0] = members.get(i).getName();
			line[1] = ""+members.get(i).getMemberID();
			line[2] = members.get(i).getCurrentState();
			line[3] = members.get(i).getStartTime();
			data.add(line);
		}
		tableModel = new MyTableModel(columns, data);
		
		memberTable = new JTable(tableModel);
		memberTable.setEnabled(false);
		memberTable.setAutoCreateRowSorter(true);
		scrollPane = new JScrollPane(memberTable);
		scrollPane.getViewport().setBackground(Color.WHITE);
		panel.add(scrollPane);
		this.setLayout(new BorderLayout());
		this.add(panel, BorderLayout.CENTER);
		
		popupMenu = new JPopupMenu();
		addMemberMenuItem = new JMenuItem("Add Member");
		editMemberMenuItem = new JMenuItem("Edit Member");
		searchMemberMenuItem = new JMenuItem("Search Member");
		deleteMemberMenuItem = new JMenuItem("Delete Member");
		popupMenu.add(addMemberMenuItem);
		popupMenu.add(editMemberMenuItem);
		popupMenu.add(searchMemberMenuItem);
		popupMenu.add(deleteMemberMenuItem);
		
		// add mouse listener
		scrollPane.addMouseListener(new MouseAdapter(){
			public void mousePressed(MouseEvent me){
				if (me.isPopupTrigger())
					popupMenu.show(me.getComponent(), me.getX(), me.getY());
			}
			
			public void mouseReleased(MouseEvent me){
				if (me.isPopupTrigger())
					popupMenu.show(me.getComponent(), me.getX(), me.getY());
			}
			
		});
		
		// add handlers
		addHandler = new AddHandler();
		editHandler = new EditHandler();
		searchHandler = new SearchHandler();
		deleteHandler = new DeleteHandler();
		addMemberMenuItem.addActionListener(addHandler);
		editMemberMenuItem.addActionListener(editHandler);
		searchMemberMenuItem.addActionListener(searchHandler);
		deleteMemberMenuItem.addActionListener(deleteHandler);
		
		// size and locate the dialog at the center of the screen
		int w = getWidth();
		int h = getHeight();
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension screenSize = tk.getScreenSize();
		int screenWidth = screenSize.width;
		int screenHeight = screenSize.height;
		int x = (screenWidth - w)/2;
		int y = (screenHeight - h)/2;
		setLocation(x,y);
		
	}
	
	
	// sort membership table
	public void sortTable(String columnName){
		
	}
	
	/** for test
	public static void main(String[] args){
		membershipFrame temp = new membershipFrame(new memberManager());
		temp.setVisible(true);
	}
	*/
	
	class MyTableModel extends AbstractTableModel{
		
		private String[] columns;
		private Vector<String[]> data;
		
		public MyTableModel(String[] initCol, Vector<String[]> initData){
			columns = initCol;
			data = initData;
		}
		@Override
		public int getColumnCount() {
			// TODO Auto-generated method stub
			return columns.length;
		}

		@Override
		public int getRowCount() {
			// TODO Auto-generated method stub
			return data.size();
		}

		@Override
		public String getValueAt(int row, int col) {
			// TODO Auto-generated method stub
			return data.get(row)[col];
		}
		
		public void setValueAt(String s, int row, int col) {
			// TODO Auto-generated method stub
			data.get(row)[col] = s;
		}
		
		public String getColumnName(int col){
			return columns[col];
		}
		
		// for add
		public void add(member m){
			String[] newRow = new String[4];
			newRow[0] = m.getName();
			newRow[1] = ""+m.getMemberID();
			newRow[2] = m.getCurrentState();
			newRow[3] = m.getStartTime();
			data.add(newRow);
		}
		
		// for edit
		public void update(member m){
			for (int i=0; i<data.size(); i++)
				if (data.get(i)[1].equals(""+m.getMemberID()))
					data.get(i)[2] =  m.getCurrentState();
		}
		
		// for delete
		public void remove(member m){
			for (int i=0; i<data.size(); i++)
				if (data.get(i)[1].equals(m.getMemberID()+""))
					data.remove(i);
		}
		
	}
	
	class AddHandler implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			addMemberDialog temp = new addMemberDialog(membershipFrame.this, mm);
			temp.setVisible(true);
		}
		
	}
	
	// add new row in the member table
	public void addMember(member m){
		tableModel.add(m);
		tableModel.fireTableDataChanged();
	}
	
	class EditHandler implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			Icon icon = null;
			String[] memberNames = new String[mm.getMembers().size()];
			for (int i=0; i<memberNames.length; i++)
				memberNames[i] = mm.getMembers().get(i).getName();
			String memberName = (String) JOptionPane.showInputDialog(
					membershipFrame.this, "Member to edit: ", 
					"Edit Member", JOptionPane.PLAIN_MESSAGE, 
					icon, memberNames, "");
			if (memberName.equals(""))
				JOptionPane.showMessageDialog(membershipFrame.this, 
						"Error: Invalid Name", "Error", JOptionPane.ERROR_MESSAGE);
			else{
			editMemberDialog temp = new editMemberDialog(membershipFrame.this, mm, memberName);
			temp.setVisible(true);
			}
		}
		
	}
	
	public void editMember(member m){
		tableModel.update(m);
		tableModel.fireTableDataChanged();
	}
	class SearchHandler implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			String name = JOptionPane.showInputDialog(membershipFrame.this, 
					"Member Name: ", "Search", JOptionPane.PLAIN_MESSAGE);
			String memberid = JOptionPane.showInputDialog(membershipFrame.this, 
					"Member ID: ", "Search", JOptionPane.PLAIN_MESSAGE);
			boolean find = false;
			for (int i=0; i<mm.getMembers().size(); i++)
				if (mm.getMembers().get(i).getName().equals(name)
						&& (mm.getMembers().get(i).getMemberID()+"").equals(memberid)){
					find = true;
					member temp = mm.getMembers().get(i);
					JOptionPane.showMessageDialog(membershipFrame.this, 
							temp.toString(), "Member Info", JOptionPane.PLAIN_MESSAGE);
				}
			if (!find)
				JOptionPane.showMessageDialog(membershipFrame.this, 
						"Error: Invalid Member", "Error", JOptionPane.ERROR_MESSAGE);
		}
		
	}
	class DeleteHandler implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			String name = JOptionPane.showInputDialog(membershipFrame.this, 
					"Member Name: ", "Delete Member", JOptionPane.PLAIN_MESSAGE);
			String memberid = JOptionPane.showInputDialog(membershipFrame.this, 
					"Member ID: ", "Delete Member", JOptionPane.PLAIN_MESSAGE);
			String id = JOptionPane.showInputDialog(membershipFrame.this, 
					"ID: ", "Delete Member", JOptionPane.PLAIN_MESSAGE);
			boolean find = false;
			for (int i=0; i<mm.getMembers().size(); i++)
				if (mm.getMembers().get(i).getName().equals(name)
						&& (mm.getMembers().get(i).getMemberID()+"").equals(memberid)
						&& (mm.getMembers().get(i).getId()).equals(id)){
					find = true;
					member temp = mm.getMembers().get(i);
					int selected = JOptionPane.showConfirmDialog(membershipFrame.this, 
							"Are you sure to delete this member?", "Delete Member", 
							JOptionPane.YES_NO_OPTION);
					if (selected == JOptionPane.YES_OPTION){
						mm.update(temp);
						mm.deleteMember(i);
						tableModel.remove(temp);
						tableModel.fireTableDataChanged();
					}
				}
			if (!find)
				JOptionPane.showMessageDialog(membershipFrame.this, 
						"Error: Invalid Operation", "Error", JOptionPane.ERROR_MESSAGE);
		}
		
	}
	
}
